home *** CD-ROM | disk | FTP | other *** search
- Path: mother.usf.edu!news
- From: Chris Frenck <cfrenck@soleil.acomp.usf.edu>
- Newsgroups: comp.lang.c
- Subject: Command line arg and segmentation fault
- Date: Fri, 02 Feb 1996 15:27:36 -0500
- Organization: University of South Florida
- Message-ID: <311273B8.25BF@soleil.acomp.usf.edu>
- NNTP-Posting-Host: ppp43.cfr.usf.edu
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0GoldB1 (Win95; I)
- CC: cfrenck@soleil.acomp.usf.edu
-
- I am a beginning C student and have run into a bit of a problem. When compiling on GCC
- (required) I get a segmentation fault. If I compile with MSVC++ at home it runs fine.
- Could someone please tell me what the error is?
-
- TIA, Chris cfrenck@soleil.acomp.usf.edu
-
- Here is my code:
-
- #include <stdio.h>
- #include <string.h>
-
- #define MAX 30 /* Maximum reversible argument length */
-
-
- /* Function Prototype */
- void check(int argc,char *argv[]); /*will move later */
-
-
- main(int argc, char *argv[])
- {
- check(argc,argv);
- return(0);
- }
-
- /***Start check function ****************************************/
- /* This function will ensure that command line arguments have */
- /* been input and directed */
- /****************************************************************/
- void check(int argc,char *argv[])
- {
- /* Variable declarations */
- char fwd[]="forward";
- char bkwd[]="backward";
- int count; /* How many command line arguments */
-
- /*Function Prototype */
- void forward(int count,char *argv[]);
- void reverse(int count,char *argv[]);
-
- if (argc > 1)
- { for (count = 1; count < argc; count++); /* counts arguments */
- }
-
- else
- printf("No command line arguments to process.");
-
- if ((strcmp(argv[1],fwd)!=0) && (strcmp(argv[1],bkwd)!=0))
- { printf("\nYou must enter either forward or backward ");
- printf("as your first argument after the file name.\n");
- printf("Please Try it again.");
- }
-
- if (strcmp(argv[1],fwd)==0) /* if 2nd argument is forward */
- forward(count,argv);
-
-
- if (strcmp(argv[1],bkwd)==0)/* if 2nd argument is backward */
- reverse(count,argv);
-
- return;
- }
- /***End check funtion *******************************************/
-
- /***Start forward function **************************************/
- /* This function will print the command line arguments forwards */
- /****************************************************************/
- void forward(int count,char *argv[])
- {
-
- int tempctr;
-
- for (tempctr=2; tempctr < count; tempctr++)
- { printf("%s ", argv[tempctr]);
- }
-
- return;
-
- }
- /***End forward function ****************************************/
-
- /***Start reverse function **************************************/
- /* This funtion will print the command line arguments in reverse*/
- /* For example: one two => two one */
- /****************************************************************/
- void reverse(int count,char *argv[])
- {
- /* Variable Declarations */
- char *argument;
- int tempctr;
- char *revphrase;
-
- /* Function Prototype */
- char palin(char [], char []);
-
- for (tempctr=count; count > 1; count--)
- { argument= argv[count];
- palin(argument,revphrase);
- printf("%s ", revphrase);
- tempctr++;
- }
-
- return;
-
- }
- /***End reverse function ****************************************/
-
-
- /***Start palin function ****************************************/
- /* This function will actually reverse lettering i.e.,the=>eht */
- /****************************************************************/
- char palin(char *argument, char *revphrase)
- {
- /* Variable declarations */
- int ctr1=0;
- int ctr2=0;
- /*SEGMENTATION FAULT IN FOR LOOP */
- /* I also get a seg. fault if just */
- /* the program name is entered and */
- /* nothing else */
-
- for (ctr1 = strlen (argument)-1; ctr1 >=0; ctr1--) /*reverses*/
- { revphrase[ctr1]= argument[ctr2];
- ctr2++;
- }
- revphrase[ctr2]='\0';
-
- return(*revphrase);
-
- }
- /*** End palin function *****************************************/
-